package com.cml.learn.netty;

import java.util.Iterator;
import java.util.Map;

public class JvmHook {
	public static void main(String[] args) {
		System.out.println(11);
		new Thread() {
			public void run() {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("222 threadId==>" + Thread.currentThread().getId());
			};
		}.start();

		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				System.out.println("jvm hook!! threadId==>" + Thread.currentThread().getId());
				System.out.println("=======hook=================================");
				Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();

				Iterator<Thread> it = threadMap.keySet().iterator();

				while (it.hasNext()) {
					Thread t = it.next();
					System.out.println(t.getId() + ":" + t.getName());
				}
				System.out.println("=======hook=================================");
			}
		});
		System.out.println("========================================");
		Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();

		Iterator<Thread> it = threadMap.keySet().iterator();

		while (it.hasNext()) {
			Thread t = it.next();
			System.out.println(t.getId() + ":" + t.getName());
		}
		System.out.println("========================================");
	}
}
